﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WebUI.Models.Business;

namespace WebUI.Areas.Admin.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /Admin/User/

        public ActionResult List()
        {
            return View();
        }

        #region 登录注销
        public ActionResult Login()
        {
            FormsAuthentication.SignOut();
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Login(UserLogin model)
        {
            if (!string.IsNullOrWhiteSpace(model.LoginName) && !string.IsNullOrWhiteSpace(model.LoginPwd))
            {
                using (var db = new WebUI.Models.Tables.MainContext())
                {
                    var user = db.Users.FirstOrDefault(t => t.LoginName == model.LoginName && t.LoginPwd == model.LoginPwd);
                    if (user != null)
                    {
                        //创造票据
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(user.ID.ToString(), false, 30);
                        //加密票据
                        string ticString = FormsAuthentication.Encrypt(ticket);
                        //输出到客户端
                        Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, ticString));
                        //跳转到登录前页面
                        return Json(true);
                    }
                }

            }
            return Json(false);
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return Redirect(FormsAuthentication.LoginUrl);
        }
        #endregion

    }
}
